cap log close 
log using "${logpath}denial_analysis.log", replace

/*******************************************************************************
denial_analysis.do

This code cleans and analyzes the ride-level panel with denials
*******************************************************************************/

clear all

qui {
	use ambulance_id FROM TO hcpcs_code NPI month year STATE denied ///
		using "${rawdatapath}ridelevel_data_deny_complete.dta" ///
		if hcpcs_code=="A0428" & year>=2011, clear

	bysort ambulance_id: gen duplicate = _N
	drop if duplicate > 2
	drop duplicate

	rename FROM from
	rename TO to

	gen orig_cat = "Diagnostic Site" if from =="D"
	replace orig_cat = "Residential Facility" if from =="E"
	replace orig_cat = "Hospital-Based ESRD Facility" if from =="G"
	replace orig_cat = "Hospital" if from =="H"
	replace orig_cat = "Transfer Site" if from =="I"
	replace orig_cat = "Freestanding ESRD Facility" if from =="J"
	replace orig_cat = "Skilled Nursing Facility" if from =="N"
	replace orig_cat = "Physician's Office'" if from =="P"
	replace orig_cat = "Residence" if from =="R"
	replace orig_cat = "Scene of Acute Event" if from =="S"
	replace orig_cat = "Intermediate Stop at Physicians Office" if from =="X"
	replace orig_cat = "Other" if missing(orig_cat) & !missing(from)

	gen orig_cat2 = orig_cat
	replace orig_cat2 = "Home" if orig_cat == "Residential Facility" | orig_cat == "Skilled Nursing Facility" | orig_cat == "Residence"
	replace orig_cat2 = "ESRD" if orig_cat == "Hospital-Based ESRD Facility" | orig_cat == "Freestanding ESRD Facility"
	replace orig_cat2 = "Physician's Office" if orig_cat == "Intermediate Stop at Physician's Office"
	replace orig_cat2 = "Other" if orig_cat2 != "Home" & orig_cat2 != "ESRD" & orig_cat2 != "Physician's Office" & orig_cat2 != "Hospital" & !missing(orig_cat2)

	gen dest_cat = "Diagnostic Site" if to =="D"
	replace dest_cat = "Residential Facility" if to =="E"
	replace dest_cat = "Hospital-Based ESRD Facility" if to =="G"
	replace dest_cat = "Hospital" if to =="H"
	replace dest_cat = "Transfer Site" if to =="I"
	replace dest_cat = "Freestanding ESRD Facility" if to =="J"
	replace dest_cat = "Skilled Nursing Facility" if to =="N"
	replace dest_cat = "Physician's Office'" if to =="P"
	replace dest_cat = "Residence" if to =="R"
	replace dest_cat = "Scene of Acute Event" if to =="S"
	replace dest_cat = "Intermediate Stop at Physicians Office" if to =="X"
	replace dest_cat = "Other" if missing(dest_cat) & !missing(to)

	gen dest_cat2 = dest_cat
	replace dest_cat2 = "Home" if dest_cat == "Residential Facility" | dest_cat == "Skilled Nursing Facility" | dest_cat == "Residence"
	replace dest_cat2 = "ESRD" if dest_cat == "Hospital-Based ESRD Facility" | dest_cat == "Freestanding ESRD Facility"
	replace dest_cat2 = "Physician's Office" if dest_cat == "Intermediate Stop at Physician's Office"
	replace dest_cat2 = "Other" if dest_cat2 != "Home" & dest_cat2 != "ESRD" & dest_cat2 != "Physician's Office" & dest_cat2 != "Hospital" & !missing(dest_cat2)

	gen date=mdy(month,1,year)
	format date %d
	gen t=ym(year,month)
	format date %tm

	label define dates 15341 "2002" 15706 "2003" 16071 "2004" 16802 "2006" 17532 "2008" 17898 "2009" 18263 "2010" 18993 "2012" 19724 "2014" 20089 "2015" 20454 "2016" 21185 "2018"
	label values date dates

	/*Defining prior authorization status*/
	rename STATE state
	destring state, replace
	gen priorauth_date=0 if !missing(state)
	replace priorauth_date=1 if state==34 | state==42 | state==45
	replace priorauth_date=2 if state==10 | state==11 | state==24 | state==37 | state==51 | state==54
	gen prior_auth = 0 if !missing(state)
	replace prior_auth = 1 if priorauth_date==1 & date>=20058
	replace prior_auth = 1 if priorauth_date==2 & date>=20454

	gen NE_hometodial=((orig_cat2=="Home" & dest_cat2=="ESRD") | (dest_cat2=="Home" & orig_cat2=="ESRD"))
	keep if NE_hometodial==1
}

preserve
collapse (mean) denied, by(priorauth_date date year)
sort date
label values date dates
twoway (line denied date if priorauth_date==1) ///
	(line denied date if priorauth_date==2, lpattern(longdash_dot)) ///
	(line denied date if priorauth_date==0, lpattern(shortdash)) ///
	(pcarrowi .225 19950 .225 20050, color(black) ) ///
	(pcarrowi .225 20570 .225 20470, color(black) ), ///
	xline(20058 20454) text( .225 19950 "1st Reg.", place(w) size(small)) ///
	text( .225 20570 "2nd Reg.", place(e) size(small)) ///
	xti(" ") yti("Share Denied") ///
	xlab(18993 19724 20454 21185, val) ///
	ylab(, format(%9.0gc)) ///
	xline(20058 20454) ///
	graphregion(color(white)) bgcolor(white) ///
	leg(subti("Patient State of Residence") ///
		order(1 "NJ, SC, PA" 2 "DE, DC, MD, NC, VA, WV" 3 "No Prior Auth."))
qui graph export "${outpath}Figure_10.pdf", as(pdf) replace // Figure 10
restore

su denied if date>=dofm(ym(2013,11)) & date<dofm(ym(2014,11)) & priorauth_date==1 // Referenced in Section 6.4, Paragraph 1 and Appendix Section J.2, Bullet 5
tab date if date>=dofm(ym(2014,11)) & date<=dofm(ym(2015,2)) & priorauth_date==1, su(denied) me // Referenced in Section 6.4, Paragraph 1 and Appendix Section J.2, Bullet 5

qui {
	collapse (sum) denied_rides=denied (count) rides=ambulance_id, by(priorauth_date state NPI prior_auth date year)

	drop if missing(NPI) | missing(state)

	gen denial_rate=denied_rides/rides
	egen firm_state=group(NPI state)
	gen t=mofd(date)

	gen months_since=t-659 if priorauth_date==1
	replace months_since=t-672 if priorauth_date==2
	replace months_since=. if months_since<-24 | months_since>23

	gen months_since100=months_since+100
	replace months_since100=50 if priorauth_date==0

	reghdfe denial_rate ib99.months_since100 if months_since100!=50, a(firm_state) vce(cluster firm_state)
}

coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopt(recast(rcap)) ///
	xti(Months Since Prior Authorization Implementation) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Firm-Level Claim Denial Rate") ///
	ylab(, angle(0)) ///
	graphregion(color(white)) bgcolor(white)
	
qui {
	graph export ${outpath}Figure_A12a.pdf, as(pdf) replace // Figure A12a

	bysort firm_state: egen balance=count(months_since100)

	reghdfe denial_rate ib99.months_since100 if balance>=48 & months_since100!=50, a(firm_state) vce(cluster firm_state)
}

coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopt(recast(rcap)) ///
	xti(Months Since Prior Authorization Implementation) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Firm-Level Claim Denial Rate") ///
	ylab(, angle(0)) ///
	graphregion(color(white)) bgcolor(white)
	
qui {
	graph export ${outpath}Figure_A12b.pdf, as(pdf) replace // Figure A12b

	gen pre_PA=months_since<0 & !missing(months_since)
	bysort firm_state pre_PA: gen pre_ct=_N if pre_PA==1
	bysort firm_state: egen pre_months=max(pre_ct)
	bysort firm_state (months_since): gen miss_next=months_since[_n+1]!=months_since[_n]+1 if !missing(months_since)
	bysort firm_state (months_since): gen post_miss=miss_next[_n-1]
	bysort firm_state (months_since): replace post_miss=max(post_miss[_n-1],post_miss[_n])

	gen plotmo=_n if _n<=48
	reghdfe denial_rate ib99.months_since100 if pre_months==24 & balance==48, a(firm_state) vce(cluster firm_state)
	gen group0_b=e(b)[1,plotmo]
	gen group0_ll=r(table)[5,plotmo]
	gen group0_ul=r(table)[6,plotmo]	

	bysort firm_state: egen exit_date=max(t)
	gen exit_at_PA=(exit_date==659 | exit_date==660) if priorauth_date==1
	replace exit_at_PA=(exit_date==672 | exit_date==673) if priorauth_date==2

	reghdfe denial_rate ib99.months_since100 if pre_months==24 & exit_at_PA==1, a(firm_state) vce(cluster firm_state)
	gen group1_b=e(b)[1,plotmo] if plotmo<=26
	gen group1_ll=r(table)[5,plotmo] if plotmo<=26
	gen group1_ul=r(table)[6,plotmo] if plotmo<=26

	reghdfe denial_rate ib99.months_since100 if pre_months==24 & exit_at_PA==0 & balance!=48 & post_miss==0, a(firm_state) vce(cluster firm_state)
	gen plotmo2=plotmo-1
	gen group2_b=e(b)[1,plotmo2] if plotmo<=36 & plotmo2>0
	gen group2_ll=r(table)[5,plotmo2] if plotmo<=36 & plotmo2>0
	gen group2_ul=r(table)[6,plotmo2] if plotmo<=36 & plotmo2>0
}

sort plotmo
twoway (connected group0_b plotmo, lcolor(ebblue) msym(|)) ///
	(line group0_ll plotmo if plotmo<-1, lcolor(ebblue%30)) ///
	(line group0_ul plotmo if plotmo<-1, lcolor(ebblue%30)) ///
	(line group0_ll plotmo if plotmo>-1, lcolor(ebblue%30)) ///
	(line group0_ul plotmo if plotmo>-1, lcolor(ebblue%30)) ///
	(connected group1_b plotmo, lcolor(erose) msym(|)) ///
	(line group1_ll plotmo if plotmo<-1, lcolor(erose%30)) ///
	(line group1_ul plotmo if plotmo<-1, lcolor(erose%30)) ///
	(line group1_ll plotmo if plotmo>-1, lcolor(erose%30)) ///
	(line group1_ul plotmo if plotmo>-1, lcolor(erose%30)) ///
	(connected group2_b plotmo, lcolor(ebg) msym(|)) ///
	(line group2_ll plotmo if plotmo<-1, lcolor(ebg%30)) ///
	(line group2_ul plotmo if plotmo<-1, lcolor(ebg%30)) ///
	(line group2_ll plotmo if plotmo>-1, lcolor(ebg%30)) ///
	(line group2_ul plotmo if plotmo>-1, lcolor(ebg%30)), ///
	xti(Months Since Prior Authorization Implementation) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Firm-Level Claim Denial Rate") ///
	ylab(, angle(0)) leg(off) ///
	graphregion(color(white)) bgcolor(white)
qui graph export ${outpath}Figure_A12c.pdf, as(pdf) replace // Figure A12c

tab exit_at_PA if date>=dofm(ym(2013,11)) & date<dofm(ym(2014,11)) & priorauth_date==1, su(denial_rate) // Referenced in Section 6.4, Paragraph 1 and Appendix Section J.2, Bullet 5
tab date exit_at_PA if date>=dofm(ym(2014,11)) & date<=dofm(ym(2015,2)) & priorauth_date==1, su(denial_rate) me // Referenced in Section 6.4, Paragraph 1 and Appendix Section J.2, Bullet 5

log close